package com.ibm.tivoli.swat.tsam_ec2.ec2;

import java.io.ByteArrayOutputStream;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import com.amazonaws.ec2.DescribeAvailabilityZonesResponseType;
import com.amazonaws.ec2.DescribeImagesResponseType;
import com.amazonaws.ec2.DescribeInstancesResponseType;
import com.amazonaws.ec2.DescribeKeyPairsResponseType;
import com.amazonaws.ec2.DescribeSecurityGroupsResponseType;
import com.amazonaws.ec2.RebootInstancesResponseType;
import com.amazonaws.ec2.RunInstancesResponseType;
import com.amazonaws.ec2.TerminateInstancesResponseType;

@Path("TSAMEC2")
public class TSAMEC2Rest {
	private TSAMEC2 tsamec2 = TSAMEC2Impl.getInstance();

	public byte[] marshall(Object object) throws JAXBException {
		JAXBContext context = JAXBContext.newInstance("com.amazonaws.ec2");
		Marshaller marshaller = context.createMarshaller();
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		marshaller.marshal(object, stream);
		return stream.toByteArray();
	}

	@POST
	@Path("DescribeImages")
	@Produces("application/xml")
	public byte[] describeImages() {
		try {
			DescribeImagesResponseType response = tsamec2.describeImages();
			return marshall(response);
		} catch (Exception e) {
			System.err.println("TSAMEC2Rest.describeImages error: " + e);
		}
		return null;
	}

	@POST
	@Path("DescribeInstances")
	@Produces("application/xml")
	public byte[] describeInstances() {
		try {
			DescribeInstancesResponseType response = tsamec2
					.describeInstances();
			return marshall(response);
		} catch (Exception e) {
			System.err.println("TSAMEC2Rest.describeInstances error: " + e);
		}
		return null;
	}

	@POST
	@Path("RebootInstances/{InstanceId.01}")
	@Produces("application/xml")
	public byte[] rebootInstances(@PathParam("InstanceId.01") String instanceId) {
		try {
			RebootInstancesResponseType response = tsamec2
					.rebootInstances(instanceId);
			return marshall(response);
		} catch (Exception e) {
			System.err.println("TSAMEC2Rest.rebootInstances error: " + e);
		}
		return null;
	}

	// http://localhost:8080/tsam_ec2/TSAMEC2/DescribeKeyPairs?AWSAccessKeyId=PMRDPCAUSR&
	// Action=DescribeKeyPairs&Signature=T94OU4zTGoyFkKUyy4SX1vX%2FG7A%3D&SignatureVersion=1&
	// Timestamp=2011-04-06T17%3A17%3A28&Version=2011-01-01
	@POST
	@Path("DescribeKeyPairs")
	@Produces("application/xml")
	public byte[] describeKeyPairs() {
		try {
			DescribeKeyPairsResponseType response = tsamec2.describeKeyPairs();
			return marshall(response);
		} catch (Exception e) {
			System.err.println("TSAMEC2Rest.describeKeyPairs error: " + e);
		}
		return null;

	}

	// http://localhost:8080/tsam_ec2/TSAMEC2/DescribeAvailabilityZones?AWSAccessKeyId=PMRDPCAUSR&
	// Action=DescribeAvailabilityZones&Signature=18AykGsXuHDF1wVg4lWo6z4Tgy4%3D&
	// SignatureVersion=1&Timestamp=2011-04-06T17%3A22%3A45&Version=2011-01-01
	@POST
	@Path("DescribeAvailabilityZones")
	@Produces("application/xml")
	public byte[] describeAvailabilityZones() {
		try {
			DescribeAvailabilityZonesResponseType response = tsamec2
					.describeAvailabilityZones();
			return marshall(response);
		} catch (Exception e) {
			System.err.println("TSAMEC2Rest.describeAvailabilityZones error: "
					+ e);
		}
		return null;

	}

	// http://localhost:8080/tsam_ec2/TSAMEC2/DescribeSecurityGroups?AWSAccessKeyId=PMRDPCAUSR&
	// Action=DescribeSecurityGroups&Signature=cT5MY6pnUWszeiJF5Ob70DW0MT4%3D&SignatureVersion=1&
	// Timestamp=2011-04-06T17%3A25%3A30&Version=2011-01-01
	@POST
	@Path("DescribeSecurityGroups")
	@Produces("application/xml")
	public byte[] describeSecurityGroups() {
		try {
			DescribeSecurityGroupsResponseType response = tsamec2
					.describeSecurityGroups();
			return marshall(response);
		} catch (Exception e) {
			System.err
					.println("TSAMEC2Rest.describeSecurityGroups error: " + e);
		}
		return null;

	}

	// http://localhost:8080/tsam_ec2/TSAMEC2/RunInstances?AWSAccessKeyId=PMRDPCAUSR&Action=RunInstances&
	// AddressingType=public&ImageId=20862&InstanceType=m1.small&MaxCount=1&MinCount=1&
	// Signature=%2FnuVnCi9avVTIEGgvA9r6euRdGI%3D&SignatureVersion=1&Timestamp=2011-04-06T17%3A29%3A48&
	// Version=2011-01-01
	@POST
	@Path("RunInstances")
	@Produces("application/xml")
	public byte[] runInstances(@FormParam("ImageId") String imageId) {
		try {
			System.out.println("TSAMEC2Rest.runInstances imageId: " + imageId);
			RunInstancesResponseType response = tsamec2.runInstances(imageId);
			return marshall(response);
		} catch (Exception e) {
			System.err.println("TSAMEC2Rest.runInstances error: " + e);
			e.printStackTrace();
		}
		return null;
	}

	@POST
	@Path("TerminateInstances")
	@Produces("application/xml")
	public byte[] terminateInstances(
			@FormParam("InstanceId.01") String instanceId) {
		try {
			TerminateInstancesResponseType response = tsamec2
					.terminateInstances(instanceId);
			return marshall(response);
		} catch (Exception e) {
			System.err.println("TSAMEC2Rest.terminateInstances error: " + e);
		}
		return null;
	}

}
